package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Notice;
import com.admission.service.NoticeManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class NoticeAdminAction extends CrudActionSupport<Notice> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2518594971176671214L;

	private NoticeManager noticeManager;
	private String id;
	private String ids;
	private Notice entity;
	private Page<Notice> page = new Page<Notice>(PageUtils.DEFAULT_PAGE_SIZE);
	

	public Notice getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("inputTime");
			page.setOrder(Page.DESC);
		}
		page = noticeManager.searchNotice(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		try{
			noticeManager.saveNotice(entity);
			// this.addActionMessage("操作成功");
		} catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	    }
		return this.list();
	}

	@Override
	public String delete() throws Exception {
		try{
			noticeManager.deleteNotcieByIds(ids);
			 //this.addActionMessage("操作成功");
		   } catch (Exception e) {
			   this.addActionMessage("操作失败，请重新操作");
		   }  
		return this.list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Notice();
		} else {
			entity = noticeManager.getById(id);
		}
	}

	@Autowired
	public void setNoticeManager(NoticeManager noticeManager) {
		this.noticeManager = noticeManager;
	}

	public Notice getEntity() {
		return entity;
	}

	public void setEntity(Notice entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<Notice> getPage() {
		return page;
	}

	public void setPage(Page<Notice> page) {
		this.page = page;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	

	
}
